<?php
include '../../../wp-blog-header.php';
require_once('rss_fetch.inc');
$url = 'http://feed43.com/3166771647616304.xml';
$rss = fetch_rss($url);
$fetched_values = explode('~',$rss->items['0']['description']);
$brand_id = '328';
$deal_link = $rss->items['0']['link'];

$final_values = explode('$',$fetched_values['1']);

/*
echo '<pre>';
print_r($final_values);
echo '</pre>';
exit();
*/

$columns_deal['name'] = pods_sanitize(ucwords(strtolower($final_values['1'])));
$columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
preg_match('@src="([^"]+)"@' , $final_values['0'] , $match);
$ext_img_url = $match['1'];

$deal = new Pod('deal');
$where = 't.camp_link = "'.$columns_deal['camp_link'].'" OR (t.ext_image_url = "'.$ext_img_url.'" AND t.daily = "1")';
$deal->findRecords('id DESC', 1,$where);
$dealsFound = $deal->getTotalRows();
if($dealsFound == 0)
{
  $cat_id = check_daily_category($columns_deal['name']);
  if(!empty($cat_id))
  {
    $columns_deal['camp_cat'] = $cat_id;
  }
  
  $columns_deal['city'] = '2';//Delhi
  
  $shortened_url = make_bitly_url($deal_link);
  if(empty($shortened_url))
  {
    $shortened_url = make_goog_url($deal_link);
  }
  $columns_deal['extlink'] = $shortened_url;
  
  $time = explode(':',trim($final_values['2']));
  $time = ($time['0']*60*60) + ($time['1']*60) + $time['2'];
  
  $columns_deal['camp_end'] = date('Y-m-d H:i:s',strtotime('+'.$time.' second'));
  $columns_deal['camp_start'] = date('Y-m-d H:i:s');
  $columns_deal['brand'] = $brand_id;
  $columns_deal['daily'] = '1';
  $columns_deal['oriprice'] = $final_values['3'];
  $discount = explode('%',$final_values['4']);

  $columns_deal['disper'] = $discount['0'];
  $columns_deal['disprice'] = $final_values['5'];
  $columns_deal['ext_image_url'] = $ext_img_url;

  $api = new PodAPI();
  $params = array('datatype' => 'deal', 'columns' => $columns_deal);
  
  /*
  echo '<pre>';
  print_r($params);
  echo '</pre>';
  exit();
  */
  
  
  $api->save_pod_item($params);
}
?>




